| Conditions | 6 |
| Paths | 17 |
| Total Lines | 81 |
| Lines | 81 |
| Ratio | 100 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /* TODO |
||
| 66 | View Code Duplication | app.get('/grades', function (req, res, next) {
|
|
| 67 | if (!req.query.id || !req.query.pwd || (req.query.sem && !(/^20\d{2}-20\d{2}-[1-2]$/).test(req.query.sem))) {
|
||
| 68 | res.send({ error: "参数不正确" });
|
||
| 69 | return; |
||
| 70 | } |
||
| 71 | if (fullLog) {
|
||
| 72 | var start = new Date(); |
||
| 73 | console.log((timeStamp() + 'Started to query the grades: ').cyan + req.query.id.yellow); |
||
|
1 ignored issue
–
show
|
|||
| 74 | } |
||
| 75 | access.login(req.query.id, req.query.pwd, res, function (headers, ires) {
|
||
| 76 | fullLog && console.log((timeStamp() + 'Successfully logged in.').green); |
||
|
1 ignored issue
–
show
|
|||
| 77 | |||
| 78 | var ret = {};
|
||
| 79 | var $ = cheerio.load(ires.text); |
||
| 80 | |||
| 81 | ret.name = escaper.unescape($('.block1text').html()).match(/姓名:.+</)[0].substring(3).replace(/</, '');
|
||
| 82 | ret.id = req.query.id; |
||
| 83 | |||
| 84 | // 实际上xnxq01id为空的时候和GET这个URL的效果是一样的,都是查询所有学期 |
||
| 85 | superagent |
||
| 86 | .post('http://csujwc.its.csu.edu.cn/jsxsd/kscj/yscjcx_list')
|
||
| 87 | .set(headers) |
||
| 88 | .type('form')
|
||
| 89 | .send({
|
||
| 90 | xnxq01id: req.query.sem |
||
| 91 | }) |
||
| 92 | .end(function (err, iires) {
|
||
| 93 | if (err) {
|
||
| 94 | console.log((timeStamp() + 'Failed to get grades page\n' + err.stack).red); |
||
|
1 ignored issue
–
show
|
|||
| 95 | res.send({ error: '无法进入成绩页面' });
|
||
| 96 | return next(err); |
||
| 97 | } |
||
| 98 | fullLog && console.log((timeStamp() + 'Successfully entered grades page.').green); |
||
| 99 | |||
| 100 | $ = cheerio.load(iires.text); |
||
| 101 | |||
| 102 | ret.grades = {};
|
||
| 103 | ret['subject-count'] = 0; |
||
| 104 | ret.failed = {};
|
||
| 105 | ret['failed-count'] = 0; |
||
| 106 | |||
| 107 | // 获取成绩列表,如果有补考记录,则以最高分的为准 |
||
| 108 | $('#dataList tr').each(function (index) {
|
||
| 109 | if (index === 0) {
|
||
| 110 | return; |
||
| 111 | } |
||
| 112 | let element = $(this).find('td');
|
||
| 113 | let title = escaper.unescape(element.eq(3).text().match(/].+$/)[0].substring(1)); |
||
| 114 | |||
| 115 | let item = {};
|
||
| 116 | item.sem = escaper.unescape(element.eq(2).text()); |
||
| 117 | item.regular = escaper.unescape(element.eq(4).text()); |
||
| 118 | item.exam = escaper.unescape(element.eq(5).text()); |
||
| 119 | item.overall = escaper.unescape(element.eq(6).text()); |
||
| 120 | |||
| 121 | if (title in ret.grades) {
|
||
| 122 | if (item.overall < ret.grades[title].overall) {
|
||
| 123 | return; |
||
| 124 | } |
||
| 125 | } |
||
| 126 | |||
| 127 | ret.grades[title] = item; |
||
| 128 | ret['subject-count']++; |
||
| 129 | |||
| 130 | // 暂不考虑NaN,(字符串 < 60)为false |
||
| 131 | if (parseInt(item.overall) < 60) {
|
||
| 132 | ret.failed[title] = item; |
||
| 133 | ret['failed-count']++; |
||
| 134 | } |
||
| 135 | }); |
||
| 136 | |||
| 137 | access.logout(headers, res, function() {
|
||
| 138 | // 返回JSON |
||
| 139 | res.send(JSON.stringify(ret)); |
||
| 140 | fullLog && console.log((timeStamp() + 'Successfully logged out: ').green + req.query.id.yellow + (' (processed in ' + (new Date() - start) + 'ms)').green);
|
||
|
1 ignored issue
–
show
|
|||
| 141 | }); |
||
| 142 | }); |
||
| 143 | |||
| 144 | |||
| 145 | }); |
||
| 146 | }); |
||
| 147 | |||
| 216 |